package com.wzm.system.controller;



import com.wzm.basic.util.AjaxResult;
import com.wzm.basic.util.PageList;
import com.wzm.system.domain.DictionaryType;
import com.wzm.system.query.DictionaryTypeQuery;
import com.wzm.system.service.DictionaryDetailService;
import com.wzm.system.service.DictionaryTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryType")
public class DictionaryTypeController {

    @Autowired
    private DictionaryTypeService dictionaryTypeService;
    @Autowired
    private DictionaryDetailService dictionaryDetailService;

   @GetMapping
    @ApiOperation(value = "查询所有字典类型")
    public List<DictionaryType> findAll(){
       return dictionaryTypeService.findAll();
   }

   @GetMapping("/{id}")
    @ApiOperation(value = "查询单个字典")
    DictionaryType findOne(@PathVariable("id") Long id){
       return dictionaryTypeService.findOne(id);
   }

   @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery query){
       return dictionaryTypeService.queryPage(query);
   }

   @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除字典")
    public AjaxResult del(@PathVariable("id") Long id){
       try {
           dictionaryTypeService.del(id);
           List<Long> ids = dictionaryDetailService.findById(id);
           dictionaryDetailService.patchDel(ids);
           return new AjaxResult();
       } catch (Exception e) {
           e.printStackTrace();
           return new AjaxResult(false,"删除失败了");
       }
   }

   @PutMapping
    @ApiOperation(value = "新增或修改字典")
    public AjaxResult addOrUpdate(@RequestBody DictionaryType dictionaryType){
       try {
           if (dictionaryType.getId()==null){
               dictionaryTypeService.add(dictionaryType);
           }else {
               dictionaryTypeService.update(dictionaryType);
           }
           return new AjaxResult();
       } catch (Exception e) {
           e.printStackTrace();
           return new AjaxResult(false,"操作失败了");
       }
   }

   @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
       try {
           dictionaryTypeService.patchDel(ids);
           return new AjaxResult();
       } catch (Exception e) {
           e.printStackTrace();
           return new AjaxResult(false,"批量删除失败了");
       }
   }

}
